package com.kgc.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.entity.Brand;
import com.kgc.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
@Controller
public class BrandController {
    @Autowired
    private BrandService brandService;

    @RequestMapping(value = "gotoBrandPage")
    public String gotoBrandPage(){
        return "showPageOfBrand";
    }

    @RequestMapping(value = "showPageOfBrand")
    @ResponseBody
    public Map<String,Object> showPageOfBrand(@RequestParam(value = "page",required = false,defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "limit",required = false,defaultValue = "3") Integer pageSize,
                                              Brand brand){
        PageInfo<Brand> brandPageInfo = brandService.showPageOfBrand(brand, pageNum, pageSize);
        Map<String,Object> map= new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","请求成功！！！");
        map.put("count",brandPageInfo.getTotal());
        map.put("data",brandPageInfo.getList());
        return map;
    }

    @RequestMapping(value = "removeBrand/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map removeBrand(@PathVariable("id") Integer id){
        Integer integer = brandService.removeBrandById(id);
        Map map = new HashMap();
        if (integer==1){
            map.put("msg","删除成功！！！");
        }else {
            map.put("msg","删除失败！！！");
        }
        return map;
    }

    @RequestMapping(value = "delBrand",method = RequestMethod.GET)
    @ResponseBody
    public Map removeBrand(Integer[] ids){
        Integer integer = 0;
        for (Integer id : ids) {
            integer=brandService.removeBrandById(id);
        }
        Map map = new HashMap();
        if (integer>0){
            map.put("msg","删除成功！！！");
        }else {
            map.put("msg","删除失败！！！");
        }
        return map;
    }

    @RequestMapping(value = "addBrand",method = RequestMethod.GET)
    public String gotoAddBrand(){
        return "addBrand";
    }

    @RequestMapping(value = "addBrand",method = RequestMethod.POST,produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String addBrand(Brand brand){
        brandService.addBrand(brand);
        return "添加成功！！！";
    }

    @RequestMapping(value = "findBrand/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Brand alterBrand(@PathVariable("id") Integer id){
        return brandService.findBrandById(id);
    }

    @RequestMapping(value = "alterBrand",method = RequestMethod.POST)
    @ResponseBody
    public Map alterBrand(Brand brand){
        Integer integer = brandService.alterBrand(brand);
        Map map = new HashMap();
        if (integer>0){
            map.put("msg","修改成功！！！");
        }else {
            map.put("msg","修改失败！！！");
        }
        return map;
    }
}
